﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _1stVATCWin
{
    public partial class NhapNCC : Form
    {
        public NhapNCC()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" ||textBox5.Text == "" )
            {
                
                label7.Text = "Chưa nhập đầy đủ thông tin";
               
            }
            else
            {
             
                Config file = new Config(Application.StartupPath + "\\config.ini");
                string connectionString = file.ReadValue("server", "con");
                SqlConnection conn = new SqlConnection(connectionString);
                string sql = "select * from NHACUNGCAP where IDNCC = @IDNCC";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@IDNCC", textBox1.Text);

                conn.Open();
                SqlDataReader r = cmd.ExecuteReader();
                if (r.Read())
                {
                    label7.Text = "ID đã tồn tại";
                    r.Close();
                }
                else
                {
                    r.Close();
                    if ((MessageBox.Show("Bạn có muốn thêm dữ liệu không?", "Thông Báo", MessageBoxButtons.OKCancel)) == DialogResult.OK)
                    {
                        this.DialogResult = DialogResult.OK;

                        cmd.Parameters.Clear();
                        cmd.CommandText = "insert into NHACUNGCAP(IDNCC,tenNCC,diachiNCC,emailNCC,sodtNCC) values(@IDNCC,@tenNCC,@diachiNCC,@emailNCC,@sodtNCC)";
                        cmd.Parameters.AddWithValue("@IDNCC", textBox1.Text);
                        cmd.Parameters.AddWithValue("@tenNCC", textBox2.Text);
                        cmd.Parameters.AddWithValue("@diachiNCC", textBox3.Text);
                        cmd.Parameters.AddWithValue("@emailNCC", textBox4.Text);
                        cmd.Parameters.AddWithValue("@sodtNCC", textBox5.Text);
                        cmd.ExecuteNonQuery();
                    }
                }
                conn.Close();
            }
        }
    }
}
